import * as Util from "@/utils"
import Config from "@/utils/config"
export default {
    filters: {
        statusName: function(value, statusList) {
            for (let i = 0; i < statusList.length; i++) {
                if (statusList[i].value === value) {
                    return statusList[i].label
                }
            }
        },
        meterTypeName: function(value, statusList) {
            for (let i = 0; i < statusList.length; i++) {
                if (statusList[i].paramKey === value) {
                    return statusList[i].paramValue
                }
            }
        },
        colectName: function(value, statusList) {
            for (let i = 0; i < statusList.length; i++) {
                if (statusList[i].equiCode === value) {
                    return statusList[i].equiName + '[' + statusList[i].equiCode + ']'
                }
            }
        }
    },
    methods: {
        formarDate(type, date) {
            return Util.dateFormat(Config.searchDate.find(item => item.date == type).format, date)
        },
        formarDateStr(type, date) {
            return Util.dateFormat(Config.searchDate.find(item => item.date == type).formatStr, date)
        },
        export2Excel(tH, fi, fiNa, dataList) {
            var tHeader = tH
            var filterVal = fi
            var fileName = fiNa
            require.ensure([], () => {
                const { export_json_to_excel } = require('@/utils/Export2Excel.js')
                const list = JSON.parse(JSON.stringify(dataList));
                let sumVal = list.reduce(function (total, currentValue, currentIndex, arr) {
                  return total + currentValue.val;
                }, 0);
                list.push({
                  "buildingName":'合计',
                  "val":sumVal
                })
                const data = this.formatJson(filterVal, list)
                export_json_to_excel(tHeader, data, fileName)
            })
        },
        formatJson(filterVal, jsonData) {
            return jsonData.map(v => filterVal.map(j => v[j]))
        }
    },
    watch: {
        'dataForm.dateType' (newV, oldV) {
            let str = ''
            switch (newV) {
                case 'date':
                    str = Util.dateFormat('YYYY-mm-dd', new Date())
                    break
                case 'month':
                    str = Util.dateFormat('YYYY-mm', new Date())
                    break
                case 'year':
                    str = Util.dateFormat('YYYY', new Date())
                    break
            }
            this.dataForm.date = str
        }
    },
}
